package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/pkg"
	"github.com/gin-gonic/gin"
	"time"
)

func Longin(context *gin.Context) {
	var l request.Longin
	if err := context.ShouldBind(&l); err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return

	}
	r, err := config.AppointmentClient.GetUserInfo(config.Ctx, &__.GetUserInfoReq{
		Phone:    l.Phone,
		Password: l.Password,
		SendCode: l.SendCode,
	})
	if err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "账号禁用",
			"data": err.Error(),
		})
		return
	}
	token, _ := pkg.GetJwtToken("12")
	config.Rdb.Set(config.Ctx, "token", token, time.Hour*2)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": gin.H{
			"id":    r.Id,
			"token": token,
		},
	})
}
